package com.ruixin.base;

import com.ruixin.base.exception.BaseException;

/**
 * try-catch工具
 */
public class BaseTryCatch {

    /**
     * 带参数
     * @param tryCatch
     * @param <T>
     * @return
     * @throws BaseException
     */
    public static <T> T process(CallBackWithResult<T> tryCatch) throws BaseException {
        try{
            return tryCatch.run();
        }catch (Throwable throwable){
            throw new BaseException("BaseTryCatch.process异常",throwable);
        }
    }

    /**
     * 没有参数
     * @param tryCatch
     * @throws BaseException
     */
    public static void process(CallBack tryCatch) throws BaseException {
        try{
            tryCatch.run();
        }catch (Throwable throwable){
            throw new BaseException("BaseTryCatch.process异常",throwable);
        }
    }

    public interface CallBack {
        void run() throws Throwable;
    }

    public interface CallBackWithResult<T>{
        T run() throws Throwable;
    }
}
